package org.lql.chaos.mq.rabbitmq.consumer;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.lql.chaos.mq.rabbitmq.constant.RabbitMQConstant;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @author: liangqinglong
 * @date: 2025-08-08 13:44
 * @description: 多交换机消息消费者
 **/
@Slf4j
@Component
public class MultiExchangeMessageConsumer {

	@RabbitListener(queues = RabbitMQConstant.FANOUT_QUEUE_1)
	public void onFanoutMessage1(String context, Message message, Channel channel) {
		log.info("[FANOUT_QUEUE_1] 收到消息：{}", context);
		try {
			// 确认消息
			channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
		} catch (Exception e) {
			log.error("[FANOUT_QUEUE_1] 确认消息失败：{}", e.getMessage());
		}
	}

	@RabbitListener(queues = RabbitMQConstant.FANOUT_QUEUE_2)
	public void onFanoutMessage2(String context, Message message, Channel channel) {
		log.info("[FANOUT_QUEUE_2] 收到消息：{}", context);
		try {
			// 确认消息
			channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
		} catch (Exception e) {
			log.error("[FANOUT_QUEUE_2] 确认消息失败：{}", e.getMessage());
		}
	}

	@RabbitListener(queues = RabbitMQConstant.DIRECT_QUEUE)
	public void onDirectMessageConsumer1(String context, Message message, Channel channel) {
		log.info("[DIRECT_QUEUE_CONSUMER_1] 收到消息：{}", context);
		try {
			// 确认消息
			channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
		} catch (Exception e) {
			log.error("[DIRECT_QUEUE_CONSUMER_1] 确认消息失败：{}", e.getMessage());
		}
	}

	@RabbitListener(queues = RabbitMQConstant.DIRECT_QUEUE)
	public void onDirectMessageConsumer2(String context, Message message, Channel channel) {
		log.info("[DIRECT_QUEUE_CONSUMER_2] 收到消息：{}", context);
		try {
			// 确认消息
			channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
		} catch (Exception e) {
			log.error("[DIRECT_QUEUE_CONSUMER_2] 确认消息失败：{}", e.getMessage());
		}
	}

	@RabbitListener(queues = RabbitMQConstant.TOPIC_QUEUE_1)
	public void onTopicMessage1(String context, Message message, Channel channel) {
		// 取消息属性
		MessageProperties props = message.getMessageProperties();
		// 分别打印常见属性
		log.info("[TOPIC_QUEUE_1] 消息内容: {}", context);
		// printMessageProperties(context, props);
		try {
			// 确认消息
			channel.basicAck(props.getDeliveryTag(), false);
		} catch (Exception e) {
			log.error("[TOPIC_QUEUE_1] 确认消息失败：{}", e.getMessage());
		}
	}

	@RabbitListener(queues = RabbitMQConstant.TOPIC_QUEUE_2)
	public void onTopicMessage2(String context, Message message, Channel channel) {
		// 取消息属性
		MessageProperties props = message.getMessageProperties();
		// 分别打印常见属性
		log.info("[TOPIC_QUEUE_2] 消息内容: {}", context);
		// printMessageProperties(context, props);
		try {
			// 确认消息
			channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
		} catch (Exception e) {
			log.error("[TOPIC_QUEUE_2] 确认消息失败：{}", e.getMessage());
		}
	}

	@RabbitListener(queues = RabbitMQConstant.PERSISTENT_QUEUE)
	public void onPersistentMessage(String context, Message message, Channel channel) {
		log.info("[PERSISTENT_QUEUE] 收到消息：{}", context);
		try {
			// 确认消息
			channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
		} catch (Exception e) {
			log.error("[PERSISTENT_QUEUE] 确认消息失败：{}", e.getMessage());
		}
	}

	@RabbitListener(queues = RabbitMQConstant.ALTERNATE_QUEUE)
	public void onAlternateMessage(String context, Message message, Channel channel) {
		log.info("[ALTERNATE_QUEUE] 收到消息：{}", context);
		try {
			// 确认消息
			channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
		} catch (Exception e) {
			log.error("[ALTERNATE_QUEUE] 确认消息失败：{}", e.getMessage());
		}
	}

	private synchronized void printMessageProperties(String context, MessageProperties props) {
		log.info("  消费者标签 (ConsumerTag): {}", props.getConsumerTag());
		log.info("  投递标签 (DeliveryTag): {}", props.getDeliveryTag());
		log.info("  路由键 (RoutingKey): {}", props.getReceivedRoutingKey());
		log.info("  交换机 (Exchange): {}", props.getReceivedExchange());
		log.info("  消息ID (MessageId): {}", props.getMessageId());
		log.info("  时间戳 (Timestamp): {}", props.getTimestamp());
		log.info("  内容类型 (ContentType): {}", props.getContentType());
		log.info("  内容编码 (ContentEncoding): {}", props.getContentEncoding());
		log.info("  优先级 (Priority): {}", props.getPriority());
		log.info("  重试次数 (Redelivered): {}", props.getRedelivered());
		log.info("  头部信息 (Headers): {}", props.getHeaders());
	}


}
